package com.niiwoo.civet.account.service.dubbo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.account.dao.entity.AccountBase;
import com.niiwoo.civet.account.dao.entity.MoneyTotal;
import com.niiwoo.civet.account.dto.common.MoneyTotalDTO;
import com.niiwoo.civet.account.dto.request.QueryMoneyTotalRequestDTO;
import com.niiwoo.civet.account.dto.response.InvestmenTotalResponseDTO;
import com.niiwoo.civet.account.dto.response.QueryMoneyTotalResponseDTO;
import com.niiwoo.civet.account.enums.AccountAmountTypeEnum;
import com.niiwoo.civet.account.service.MoneyTotalDubboService;
import com.niiwoo.civet.account.service.local.AccountService;
import com.niiwoo.civet.account.service.local.MoneyTotalLocalService;
import com.niiwoo.civet.trade.service.credit.CreditDubboService;
import com.niiwoo.civet.trade.service.orgloan.OrgLoanDubboService;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liaojunjun
 */
@Service(version = "1.0.0")
public class MoneyTotalDubboServiceImpl implements MoneyTotalDubboService {

	@Autowired
	private AccountService accountService;
	
    @Autowired
    private MoneyTotalLocalService moneyTotalLocalService;
    
    @Reference(version = "1.0.0")
    private CreditDubboService creditDubboService;
    
    @Reference(version = "1.0.0")
    private OrgLoanDubboService orgLoanDubboService;

    @Override
    public void moneyTotal(MoneyTotalDTO dto) {
        moneyTotalLocalService.moneyTotal(dto);
    }

    @Override
    public QueryMoneyTotalResponseDTO queryMoneyTotal(
            QueryMoneyTotalRequestDTO dto) {
        Assert.notNull(dto, "queryMoneyTotal requestDTO is null.");
        QueryMoneyTotalResponseDTO queryMoneyTotalResponseDTO = new QueryMoneyTotalResponseDTO();
        int isExistCredit = creditDubboService.queryCreditInvestorProject(dto.getUserId());
        int isOrgLoan = orgLoanDubboService.queryOrgLoanProject(dto.getUserId());
        queryMoneyTotalResponseDTO = moneyTotalLocalService.queryMoneyTotal(dto.getUserId(),dto.getAccountId());
        queryMoneyTotalResponseDTO.setIsExistCredit(isExistCredit);
        queryMoneyTotalResponseDTO.setIsOrgLoan(isOrgLoan);
        return queryMoneyTotalResponseDTO;
    }

    @Override
    public InvestmenTotalResponseDTO getUserInvestmenTotal(String userId) {
        List<MoneyTotal> moneyTotalList = moneyTotalLocalService.getUserMoneyTotal(userId);
        InvestmenTotalResponseDTO investmenTotalResponse = new InvestmenTotalResponseDTO();
        investmenTotalResponse.setBalanceAmount(BigDecimal.ZERO);
        investmenTotalResponse.setCumulativeInterest(BigDecimal.ZERO);
        investmenTotalResponse.setCollectInterest(BigDecimal.ZERO);
        investmenTotalResponse.setCollectAmount(BigDecimal.ZERO);
        
        // 查询帐户余额
        AccountBase accountBase = accountService.getNiiwooAccount(userId, UserRoleEnum.INVESTOR);
		if (accountBase != null && accountBase.getAvailableAmount() != null) {
			investmenTotalResponse.setBalanceAmount(accountBase.getAvailableAmount());		
		}
        
        for (MoneyTotal moneyTotal : moneyTotalList) {
            AccountAmountTypeEnum amountType = AccountAmountTypeEnum.enumOf(moneyTotal.getAmountType());
            BigDecimal amount = moneyTotal.getAmount();
            switch (amountType) {
                case DUE_IN_CORPUS:
                    investmenTotalResponse.setCollectAmount(investmenTotalResponse.getCollectAmount().add(amount));
                    break;
                case DUE_IN_INTEREST:
                    investmenTotalResponse.setCollectInterest(investmenTotalResponse.getCollectInterest().add(amount));
                    break;
                case INVEST_INCOME:
                    investmenTotalResponse.setCumulativeInterest(investmenTotalResponse.getCumulativeInterest().add(amount));
                    break;
                default:
                    break;
            }
        }
        return investmenTotalResponse;
    }

    @Override
    public void moneyTotal(List<MoneyTotalDTO> dtoList) {
        moneyTotalLocalService.moneyTotal(dtoList);
    }

    /**
     * 根据类型统计总金额
     *
     * @param amountTypeEnumList
     * @return
     */
    public BigDecimal queryMoneyTotalByAmountTypeList(AccountAmountTypeEnum... amountTypeEnumList) {
        return moneyTotalLocalService.queryMoneyTotalByAmountTypeList(amountTypeEnumList);
    }

    @Override
    public BigDecimal queryMoneyByUserIdAndAccountType(String userId, AccountAmountTypeEnum amountType) {
        Assert.hasLength(userId, "查询用户指定类型资金，用户ID不可空");
        Assert.notNull(amountType, "查询用户指定类型资金，类型不可空");
        return moneyTotalLocalService.queryMoneyByUserIdAndAccountType(userId,amountType);
    }
}
